package ca.kooki.codesnippet.core;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SystemDateTest {

  @Before
  @After
  public void setUpAndTearDown()
  {
    SystemDate.reset();
  }
  
  
  
  @Test
  public void NormalNow()
  {
    Assert.assertEquals(new Date(),SystemDate.Now());
  }
  
  @Test
  public void setNowTo() throws ParseException
  {
    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
    Date newNow = format.parse("Nov 4, 2003 8:14 PM");
    SystemDate.setNowTo(newNow);
    Assert.assertEquals(newNow,SystemDate.Now());
  }
  
  @Test 
  public void createDate() throws ParseException
  {
    Date actual = SystemDate.parse("Nov 4, 2003 8:14 PM");
    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
    Date expected = format.parse("Nov 4, 2003 8:14 PM");
    
    Assert.assertEquals(expected,actual);
  }
  
  @Test
  public void setNowToFromASimpleString() throws ParseException
  {
    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
    Date newNow = format.parse("Nov 4, 2003 8:14 PM");
    SystemDate.setNowTo("Nov 4, 2003 8:14 PM");
    Assert.assertEquals(newNow,SystemDate.Now());
  }
  
  @Test
  public void formatDate() throws ParseException
  {
    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
    Date newNow = format.parse("Nov 4, 2003 8:14 PM");
    String actual = SystemDate.formatDate(newNow);
    Assert.assertEquals("Nov 4, 2003 8:14 PM",actual);
  }

  @Test
  public void areSimilarNotTheSame() throws ParseException
  {
    Date now = SystemDate.parse("Nov 4, 2003 8:14 PM");
    Date notNow = SystemDate.parse("Nov 4, 2003 8:15 PM");
    Assert.assertEquals(false, SystemDate.areSimilar(now,notNow));
  }

  
  @Test
  public void areSimilarTheSame() throws ParseException
  {
    Date now = SystemDate.parse("Nov 4, 2003 8:14 PM");
    Date alsoNow = SystemDate.parse("Nov 4, 2003 8:14 PM");
    Assert.assertEquals(true, SystemDate.areSimilar(now,alsoNow));
  }

  
  @Test
  public void areSimilarBasedToTheMinute() throws InterruptedException
  {
    Date now = new Date();
    Thread.sleep(100);
    
    Date alsoNow = new Date();
    
    Assert.assertEquals(true, SystemDate.areSimilar(now,alsoNow));
  }
  
}
